package com.hassean.we.admin.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hassean.we.admin.common.constant.CommonConstant;
import com.hassean.we.admin.mapper.TbRescueTeamMapper;
import com.hassean.we.admin.mapper.TbSimInfoMapper;
import com.hassean.we.admin.mapper.TbTerminalMapper;
import com.hassean.we.admin.model.entity.TbRescueTeam;
import com.hassean.we.admin.model.entity.TbSimInfo;
import com.hassean.we.admin.model.entity.TbTerminal;
import com.hassean.we.admin.service.TbTerminalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yajun
 * @since 2019-03-29
 */
@Service
public class TbTerminalServiceImpl extends ServiceImpl<TbTerminalMapper, TbTerminal> implements TbTerminalService {
    @Autowired
    TbTerminalMapper tbTerminalMapper;
    @Autowired
    TbSimInfoMapper tbSimInfoMapper;
    @Autowired
    TbRescueTeamMapper tbRescueTeamMapper;

    @Override
    public List<Map<String, Object>> selectTerminalIdByPhone(String phone) {

        List<Map<String, Object>> list = tbTerminalMapper.selectTerminalIdByPhone(phone);

        return  list;


    }
    @Override
    public Integer saveTbTerminal(String terminalNumber, String simNumber,String ownerName,
                               String departmentName,  String simOwnerName,
                               Long departmentId, Integer type, Integer state,String numberAttribution,
                               String ImsiCode,  String ImeiCode,  String provinces,  String municipalities,
                               String districts,  String townStreet) {
        Boolean flag = false;
        TbTerminal tbTerminal = new TbTerminal();
        TbSimInfo tbSimInfo = new TbSimInfo();
        tbSimInfo.setOwnerName(simOwnerName);//实名登记的人
        tbSimInfo.setSimNumber(simNumber);
        tbSimInfo.setDepartmentName(departmentName);//所属单位
        tbSimInfoMapper.insertAndReturnId(tbSimInfo);
        Long id =   tbSimInfo.getSimInfoId();
        if(id==0L){
            return CommonConstant.phoneRepetition;//2000电话号码重复
        }
        Long rescueTeamId = null;
        if(id!=0L){
            TbRescueTeam tbRescueTeam = new TbRescueTeam();
            tbRescueTeam.setDepartmentName(departmentName);
            tbRescueTeam.setDistricts(districts);
            tbRescueTeam.setTownStreet(townStreet);
            tbRescueTeam.setProvinces(provinces);
            tbRescueTeam.setMunicipalities(municipalities);
            tbRescueTeam.setDepartmentId(departmentId);
            tbRescueTeamMapper.insertAndReturnId(tbRescueTeam);
            rescueTeamId =  tbRescueTeam.getRescueTeamId();
        }
        if(rescueTeamId!=null){
            tbTerminal.setSimInfoId(id);
            tbTerminal.setTerminalNumber(terminalNumber);
            //判重
            EntityWrapper entityWrapper =   new EntityWrapper<>();
            entityWrapper.eq("terminal_number",terminalNumber);
            TbTerminal termina = new TbTerminal();
            termina.setTerminalNumber(terminalNumber);
            TbTerminal terminal   = tbTerminalMapper.selectOne(termina);
            if(terminal!=null){
                return CommonConstant.tbTerminalRepetition;//2001终端号码重复
            }else {
                tbTerminal.setImeiCode(ImeiCode);
                tbTerminal.setImsiCode(ImsiCode);
                tbTerminal.setState(state);
                tbTerminal.setType(type);
                tbTerminal.setRescueTeamId(rescueTeamId);//有问题
                tbTerminal.setNumberAttribution(numberAttribution);
                tbTerminal.setOwnerName(ownerName);//终端机负责人

                tbTerminalMapper.insert(tbTerminal);
                return CommonConstant.success;//1000成功
            }

        }else {
            return CommonConstant.failed ;// 2002失败;
        }
    }
}
